package com.shujia.scala

import scala.io.Source

object Demo22Student {
  def main(args: Array[String]): Unit = {

    /**
      * 统计班级的人数
      *
      */

    //1、读取数据
    val lines: List[String] = Source.fromFile("scala/data/students.txt").getLines().toList


    //2、取出班级
    val classs: List[String] = lines.map(line => line.split(",")(4))


    //3、按照班级进行分组
    val group: Map[String, List[String]] = classs.groupBy(clazz => clazz)


    //4、统计班级的人数
    val countMap: Map[String, Int] = group.map(kv => {
      val clazz: String = kv._1
      val values: List[String] = kv._2

      //计算班级的人数
      val count: Int = values.size

      (clazz, count)
    })


    //打印结果
    countMap.foreach(println)


    /**
      *
      * select clazz,count(1)  from student group by clazz
      *
      * sql 执行流程
      *
      * from  -->  join on --> where  --> group by ---> having --> select  --> order by --> limit
      *
      *
      */


  }
}
